package com.hkd.system.service.user.Impl;

import com.github.pagehelper.PageHelper;
import com.hkd.common.core.domain.R;
import com.hkd.common.core.domain.TableDataInfo;
import com.hkd.common.core.enums.ResultCode;
import com.hkd.system.domain.user.User;
import com.hkd.system.domain.user.dto.UserDTO;
import com.hkd.system.domain.user.dto.UserQueryDTO;
import com.hkd.system.domain.user.vo.UserVO;
import com.hkd.system.mapper.user.UserMapper;
import com.hkd.system.service.user.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hkd.common.core.controller.BaseController.getTableDataInfo;

@Slf4j
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public TableDataInfo list(UserQueryDTO userQueryDTO) {
        PageHelper.startPage(userQueryDTO.getPageNum(), userQueryDTO.getPageSize());
        List<UserVO> usersList = userMapper.selectUserList(userQueryDTO);
        return getTableDataInfo(usersList);
    }

    @Override
    public R updateStatus(UserDTO userDTO) {
        //查询用户信息
        User user = userMapper.selectById(userDTO.getUserId());
        if(user==null){
            log.error("用户不存在");
            return R.failed(ResultCode.USER_NOT_EXISTS);
        }
        //更新用户状态
        user.setStatus(userDTO.getStatus());
        int i = userMapper.updateById(user);
        if(i<=0){
            log.error("更新用户状态失败");
            return R.failed(ResultCode.FAILED_EXAM_USER_STATE);
        }
        return R.success();
    }
}
